/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.web.controller;

import com.wallellen.core.page.ViewMode;
import com.wallellen.core.sql.query.DBRule;
import com.wallellen.core.sql.query.DBSort;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.knowledge.doc.domain.UserMessage;
import com.wallellen.knowledge.doc.server.UsermessageServiceInter;
import com.wallellen.knowledge.web.util.ThemesUtil;
import com.wallellen.web.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户消息
 *
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 */
@RequestMapping("/webusermessage")
@Controller
public class UserMessageController extends WebUtils {
    @Resource
    private UsermessageServiceInter usermessageServiceImpl;

    /**
     * 查看用户信息
     *
     * @param id  消息ID
     * @param num 分页参数（返回消息列表需要）
     * @return
     */
    @RequestMapping("/showMessage")
    public ModelAndView showMessage(String id, Integer num) {
        try {
            usermessageServiceImpl.setRead(id);
            UserMessage usermessage = usermessageServiceImpl.getUsermessageEntity(id);
            String readstatename = "";
            if (usermessage.getReadstate().equals("0")) {//0未读、1已读
                readstatename = "未读";
            } else if (usermessage.getReadstate().equals("1")) {
                readstatename = "已读";
            }
            return ViewMode.getInstance()
                    .putAttr("usermessage", usermessage)
                    .putAttr("readstatename", readstatename)
                    .putAttr("num", num)
                    .returnModelAndView(ThemesUtil.getThemePath() + "/know/userMessage");
        } catch (Exception e) {
            return ViewMode.getInstance().setError(e.toString()).returnModelAndView("");
        }
    }

    /**
     * 显示首页未读消息
     *
     * @return Map<String,Object>
     */
    @RequestMapping("/showHomeMessage")
    @ResponseBody
    public Map<String, Object> showHomeMessage(HttpSession session) {
        try {
            DataQuery query = usermessageServiceImpl.createUsermessageSimpleQuery(null);
            query.setNoCount();
            query.addRule(new DBRule("USERMESSAGE.READUSERID", getCurrentUser(session).getId(), "="));
            query.addRule(new DBRule("USERMESSAGE.READSTATE", "0", "="));
            query.addSort(new DBSort("USERMESSAGE.CTIME", "DESC"));
            List<Map<String, Object>> list = query.search().getResultList();
            String newMessage = "";
            if (list != null && list.size() > 0) {
                newMessage = list.get(0).get("TITLE").toString();
            }

            return ViewMode.getInstance()
                    .putAttr("newMessage", newMessage)
                    .putAttr("unReadCount", list.size())
                    .returnObjMode();
        } catch (Exception e) {
            return ViewMode.getInstance().returnObjMode();
        }
    }
}
